package com.ams.util;

import java.nio.ByteBuffer;

public final class BufferUtils {

    public static ByteBuffer slice(ByteBuffer buf, int start, int end) {
        ByteBuffer b = buf.duplicate();
        b.position(start);
        b.limit(end);
        return b.slice();
    }

    public static ByteBuffer trim(ByteBuffer buf, int length) {
        ByteBuffer b = buf.duplicate();
        b.limit(b.position() + length);
        buf.position(buf.position() + length);
        return b.slice();
    }

    public static ByteBuffer[] concat(ByteBuffer[] buf1, ByteBuffer[] buf2) {
        ByteBuffer[] buf = new ByteBuffer[buf1.length + buf2.length];
        int j = 0;
        for (int i = 0, len = buf1.length; i < len; i++) {
            buf[j++] = buf1[i];
        }
        for (int i = 0, len = buf2.length; i < len; i++) {
            buf[j++] = buf2[i];
        }
        return buf;
    }

}
